package jsu.ask.util;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import com.google.gson.JsonObject;

import com.google.gson.JsonParser;

import org.neo4j.jdbc.Driver;

//Class.forName("org.neo4j.jdbc.Driver");

//Connect

public class DBUtil {

    Connection con;

    Statement stmt;

    ResultSet rs;

    public ResultSet rs(String sqlstring) throws SQLException {

        try {

            Class.forName("org.neo4j.jdbc.Driver").newInstance();

            this.con = DriverManager.getConnection("jdbc:neo4j:http://localhost:7474/","用户名","密码");

            this.stmt = con.createStatement();
            System.out.println("获取数据库连接成功");

            this.rs = stmt.executeQuery(sqlstring);
            System.out.println("执行数据库语句成功");
        } catch (InstantiationException e) {

// TODO Auto-generated catch block

            e.printStackTrace();

        } catch (IllegalAccessException e) {

// TODO Auto-generated catch block

            e.printStackTrace();

        } catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

            e.printStackTrace();

        }

        return rs;

    }

    public void close () {

        if (this.rs != null) {

            try {

                this.rs.close();

            } catch (SQLException e) {

                e.printStackTrace();

            }

        }

        if (this.stmt != null) {

            try {

                this.stmt.close();

            } catch (SQLException e) {

                e.printStackTrace();

            }

        }

        if (this.con != null) {

            try {

                this.con.close();

            } catch (SQLException e) {

                e.printStackTrace();

            }

        }

    }

//    public static void main(String[] args) throws SQLException {
//
//// TODO Auto-generated method stub
//
//        DBUtil db=new DBUtil();
//        ResultInfo resultInfo=new ResultInfo();
//        ResultSet rs=db.rs("MATCH(n:Disease) WHERE n.name='百日咳' RETURN n");
//
//        while(rs.next())
//
//        {
//            resultInfo.setCode(1);
//            resultInfo.setMessage(rs.toString());
//            String jsonstring=rs.getString("n");
//
////            System.out.println(jsonstring);
//
//            JsonObject jsonObject = (JsonObject) new JsonParser().parse(jsonstring);
//            resultInfo.setResult(jsonObject);
//            System.out.println(resultInfo.getResult());
//            System.out.println(jsonObject.get("name"));
//
//        }
//
//        db.close();
//
//    }

}